﻿// -----------------------------------------------------------------------
// <copyright file="MetalCheckBox.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Metal.Config.ExtendedControls
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    /// <summary>
    /// Custom check box control
    /// </summary>
    public class MetalCheckBox : System.Windows.Forms.CheckBox
    {
        /// <summary>Stores the attribute / setting name, where the value of the check box will be saved</summary>
        private string internalValue;

        /// <summary>Stores the resource ID</summary>
        private string strResourceID;

        private bool valueChanged;

        public MetalCheckBox()
        {
            this.Click += new EventHandler(MetalCheckBox_Click);
        }

        void MetalCheckBox_Click(object sender, EventArgs e)
        {
            valueChanged = true;
        }

        /// <summary>
        /// Gets or sets the setting name
        /// </summary>
        public string SettingName
        {
            get
            {
                return this.internalValue;
            }

            set
            {
                this.internalValue = value;
            }
        }

        /// <summary>
        /// Gets or sets the resource ID
        /// </summary>
        public string ResourceID
        {
            get
            {
                return this.strResourceID;
            }

            set
            {
                this.strResourceID = value;
            }
        }

        /// <summary>
        /// Save CheckBox value
        /// </summary>
        public void SaveSetting()
        {
            if (valueChanged)
            {
                Settings.Instance.SetValue("LastSettings." + this.SettingName, this.Checked.ToString());
            }
        }

        /// <summary>
        /// When the control becomes visible load the control value (checked / unchecked) and the text (resource)
        /// </summary>
        /// <param name="e">Event args</param>
        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);

            if (this.SettingName != null)
            {
                this.Checked = Settings.Instance.GetValue("LastSettings." + this.SettingName, false);
            }

            if (!this.DesignMode)
            {
                this.Text = Resources.MessageProvider.GetMessage(this.ResourceID);
            }
        }
    }
}
